package com.sporadic.backstage.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sporadic.backstage.dto.UserDto;
import com.sporadic.backstage.entity.User;
import com.sporadic.backstage.mapper.UserMapper;
import com.sporadic.backstage.service.UserService;
import dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据条件分页查询指定商家中所有会员
     * @param page
     * @param map 查询条件 可为空
     * @return
     */
    @Override
    public IPage<UserDTO> selUserPage(Page page, Map map) {
        System.out.println(map);
        IPage<UserDTO> userIPage = userMapper.selUserPage(page,map);
        return userIPage;
    }

    /**
     * 新增会员(用户)
     * @param user
     * @return  受影响行数 大于0添加成功 否则失败
     */
    @Override
    public Integer addUser(User user)
    {
        user.setUserAccount(String.valueOf(System.currentTimeMillis()));
        return userMapper.addUser(user);
    }

    /**
     * 根据卡号修改会员信息
     * @param user
     * @return
     */
    @Override
    public Integer upUserByUid(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        return userMapper.update(user,queryWrapper) ;
    }

    /**
     * 批量删除会员
     * @param list 要删除会员集合
     * @return
     */
    @Override
    public int delUser(List list) {
        return userMapper.delUser(list);
    }

    /**
     * 查询商家指定积分等级的总人数
     * @param merchantId 商家编号
     * @param userRrankId 积分等级编号
     * @returne 总人数
     */
    @Override
    public Integer selUserCountByRank(Integer merchantId, Integer userRrankId) {
        return userMapper.selUserCountByRank(merchantId, userRrankId);
    }

    /**
     * 分页查询商家本月消费会员
     * @param page
     * @param orderMerchantId 所属商家编号
     * @return
     */
    @Override
    public IPage<User> selByMonthUserPage(Page page,Integer orderMerchantId) {
        return userMapper.selByMonthUserPage(page,orderMerchantId);
    }
    /**
     * 分页查询商家今天生日会员信息
     * @param page
     * @param orderMerchantId
     * @return
     */
    @Override
    public IPage<User> selByBirthdayNowUserPage(Page page, Integer orderMerchantId) {
        return userMapper.selByBirthdayNowUserPage(page, orderMerchantId);
    }
    /**
     * 分页查询商家本月过生日会员信息
     * @param page
     * @param orderMerchantId
     * @return
     */
    @Override
    public IPage<User> selByBirthdayMonthUserPage(Page page, Integer orderMerchantId) {
        return userMapper.selByBirthdayMonthUserPage(page, orderMerchantId);
    }

    @Override
    public List<UserDto> findMembershipFormSkip(Integer merchantId) {
        return userMapper.findMembershipFormSkip(merchantId);
    }
}
